package com.javafactroy.kunlong.codefactroy.service.domain;

import com.javafactroy.kunlong.data.dto.MetadataColumnPropApiDto;
import com.javafactroy.kunlong.data.dto.MetadataTablePropApiDto;
import com.javafactroy.kunlong.data.service.MetadataColumnPropApiService;
import com.javafactroy.kunlong.data.service.MetadataTablePropApiService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Data
@Service
public class MetadataPropService {

    @Autowired
    MetadataTablePropApiService metadataTablePropApiService;

    @Autowired
    MetadataColumnPropApiService metadataColumnPropApiService;

    public void saveTableProp(MetadataPropAgg tableAgg) {
        delTableProp(tableAgg.getMetadataTablePropApiDto());

        saveTableProp(tableAgg.getMetadataTablePropApiDto());
        MetadataTablePropApiDto table = tableAgg.getMetadataTablePropApiDto();
        tableAgg.getColList().forEach(i -> i.setTableId(table.getTableId()).setTableName(table.getTableName()));
        saveColumnProp(tableAgg.getColList());
    }


    public void delTableProp(MetadataTablePropApiDto table) {
        MetadataTablePropApiDto dto = new MetadataTablePropApiDto();
        dto.setTableName(table.getTableName());
        metadataTablePropApiService.deleteBy(dto);

        delColumnProp(table);
    }
    public void delColumnProp(MetadataTablePropApiDto table) {

        MetadataColumnPropApiDto param = new MetadataColumnPropApiDto();
        param.setTableName(table.getTableName());
        metadataColumnPropApiService.deleteBy(param);
    }


    public void saveTableProp(MetadataTablePropApiDto table) {
        metadataTablePropApiService.insert(table);

    }

    public void saveColumnProp(List<MetadataColumnPropApiDto> columns) {
        metadataColumnPropApiService.saveBatch(columns);
    }

}
